package com.bwf.lgqingdan.gui.adapter;

import android.content.Context;
import android.graphics.Rect;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bwf.lgqingdan.R;
import com.bwf.lgqingdan.entity.ArticlesBean;
import com.bwf.lgqingdan.entity.ResponseCategoryThing;
import com.bwf.lgqingdan.entity.ThingsBean;
import com.bwf.lgqingdan.gui.adapter.base.BaseRecycleViewAdapter;
import com.bwf.lgqingdan.gui.view.NestingRecycleView;
import com.bwf.lgqingdan.mvp.view.CategoryThingView;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/11/15.
 */

public class CategoryThingMainAdapter extends BaseRecycleViewAdapter<ThingsBean> {
    private static final int TYPE_HEADER_RELATED_TAGS = 0;
    private static final int TYPE_HEADER_ARTICLES = 1;
    private static final int TYPE_HEADER_THING_SORT = 2;
    private static final int TYPE_ITEM_THING = 3;
    private CategoryThingView categoryThingView;
    private List<ResponseCategoryThing.TagDetailsRelatedTagsBean.BodyBean.RelatedTagsData.RelatedTagsBean> relatedTags;
    private List<ArticlesBean> articles;

    public void setRelatedTags(List<ResponseCategoryThing.TagDetailsRelatedTagsBean.BodyBean.RelatedTagsData.RelatedTagsBean> relatedTags) {
        if (this.relatedTags == null) {
            this.relatedTags = new ArrayList<>();
        }
        this.relatedTags.clear();
        this.relatedTags.addAll(relatedTags);
        notifyItemChanged(TYPE_HEADER_RELATED_TAGS);
    }

    public void setArticles(List<ArticlesBean> articles) {
        if (this.articles == null) {
            this.articles = new ArrayList<>();
        }
        this.articles.clear();
        this.articles.addAll(articles);
        notifyItemChanged(TYPE_HEADER_ARTICLES);
    }

    public CategoryThingMainAdapter(Context context, CategoryThingView categoryThingView) {
        super(context);
        this.categoryThingView = categoryThingView;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == TYPE_HEADER_RELATED_TAGS) {
            return TYPE_HEADER_RELATED_TAGS;
        } else if (position == TYPE_HEADER_ARTICLES) {
            return TYPE_HEADER_ARTICLES;
        } else if (position == TYPE_HEADER_THING_SORT) {
            return TYPE_HEADER_THING_SORT;
        }
        return TYPE_ITEM_THING;
    }

    @Override
    public int getHeaderItemCount() {
        return 3;
    }

    @Override
    protected int getFooterItemCount() {
        return 0;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_HEADER_RELATED_TAGS) {
            View view = inflaterView(R.layout.subview_category_thing_related_tags_header, parent);
            return new RelatedTagsHeaderViewHolder(view);
        } else if (viewType == TYPE_HEADER_ARTICLES) {
            View view = inflaterView(R.layout.layout_item_tag_details_article, parent);
            return new ArticlesViewHolder(view);
        } else if (viewType == TYPE_HEADER_THING_SORT) {
            View view = inflaterView(R.layout.layout_item_thing_sort, parent);
            return new ThingSortViewHolder(view) {
            };
        }
        View view = inflaterView(R.layout.list_item_related_thing, parent);
        return new ThingItemViewHolder(view);
    }
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int itemType = getItemViewType(position);
        switch (itemType) {
            case TYPE_HEADER_RELATED_TAGS:
                if (this.relatedTags != null && this.relatedTags.size() != 0) {
                    //如果relatedTags不为null，说明Activity已获取到了数据
                    RelatedTagsHeaderViewHolder relatedTagsHeaderViewHolder = (RelatedTagsHeaderViewHolder) holder;
                    relatedTagsHeaderViewHolder.layout.setVisibility(View.VISIBLE);
                    if (relatedTagsHeaderViewHolder.categoryThingRelatedTagsAdapter.getDatas().size() == 0) {
                        relatedTagsHeaderViewHolder.categoryThingRelatedTagsAdapter.setDatas(relatedTags);
                    } else {
                        relatedTagsHeaderViewHolder.categoryThingRelatedTagsAdapter.notifyDataSetChanged();
                    }
                }
                break;
            case TYPE_HEADER_ARTICLES:

                if (this.articles != null && this.articles.size() != 0) {
                    ArticlesViewHolder articlesViewHolder = (ArticlesViewHolder) holder;
                    articlesViewHolder.layout.setVisibility(View.VISIBLE);
                    if (articlesViewHolder.adapter.getDatas().size() == 0) {
                        articlesViewHolder.adapter.setDatas(this.articles);
                        if (this.articles.size() <= articlesViewHolder.adapter.getMaxCount()) {
                            articlesViewHolder.tvItemTagDetailsCheckAllArticle.setVisibility(View.GONE);
                        }
                    } else {
                        articlesViewHolder.adapter.notifyDataSetChanged();
                    }
                }
                break;
            case TYPE_HEADER_THING_SORT:
                if(getDatas() != null && getDatas().size() != 0){
                    ThingSortViewHolder thingSortViewHolder = (ThingSortViewHolder) holder;
                    thingSortViewHolder.layout.setVisibility(View.VISIBLE);
                }
                break;
            case TYPE_ITEM_THING:
                ThingItemViewHolder thingItemViewHolder = (ThingItemViewHolder) holder;
                ThingsBean thingsBean = getItem(position);
                thingItemViewHolder.imgItemTagDetailsThingsCover.setImageURI(thingsBean.getFeaturedImageUrl());
                thingItemViewHolder.tvItemTagDetailsThingsTitle.setText(thingsBean.getFullName().replace(thingsBean.getName(), ""));
                thingItemViewHolder.tvItemTagDetailsThingsContent.setText(thingsBean.getName());
                thingItemViewHolder.tvItemTagDetailsThingsPrice.setText(thingsBean.getPrice());
                thingItemViewHolder.tvItemTagDetailsThingsLikeCount.setText(thingsBean.getLikeCount() + "");
                break;
        }
    }

    class RelatedTagsHeaderViewHolder extends RecyclerView.ViewHolder implements OnItemClickListener<ResponseCategoryThing.TagDetailsRelatedTagsBean.BodyBean.RelatedTagsData.RelatedTagsBean> {
        @BindView(R.id.recyclerView_related_tags)
        RecyclerView recyclerViewRelatedTags;
        CategoryThingRelatedTagsAdapter categoryThingRelatedTagsAdapter;
        View layout;
        RelatedTagsHeaderViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            this.layout = view;
            LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
            layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            recyclerViewRelatedTags.setLayoutManager(layoutManager);
            categoryThingRelatedTagsAdapter = new CategoryThingRelatedTagsAdapter(getContext());
            recyclerViewRelatedTags.setAdapter(categoryThingRelatedTagsAdapter);
            categoryThingRelatedTagsAdapter.setOnItemClickListener(this);
            //设置item的间距
            int spacingInPixels = getContext().getResources().getDimensionPixelSize(R.dimen.item_space);
            recyclerViewRelatedTags.addItemDecoration(new SpaceItemDecoration(spacingInPixels));
        }

        @Override
        public void onItemClick(int position, View view, ResponseCategoryThing.TagDetailsRelatedTagsBean.BodyBean.RelatedTagsData.RelatedTagsBean data) {
            categoryThingView.clickItemRelatedTags(data);
        }

        public class SpaceItemDecoration extends RecyclerView.ItemDecoration {

            private int space;

            public SpaceItemDecoration(int space) {
                this.space = space;
            }

            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                int childAdapterPosition = parent.getChildAdapterPosition(view);
                if (childAdapterPosition == 0) {
                    outRect.left = space;
                }
            }
        }
    }

    class ThingItemViewHolder extends BaseViewHolder {
        @BindView(R.id.img_item_tag_details_things_cover)
        SimpleDraweeView imgItemTagDetailsThingsCover;
        @BindView(R.id.tv_item_tag_details_things_title)
        TextView tvItemTagDetailsThingsTitle;
        @BindView(R.id.tv_item_tag_details_things_content)
        TextView tvItemTagDetailsThingsContent;
        @BindView(R.id.tv_item_tag_details_things_price)
        TextView tvItemTagDetailsThingsPrice;
        @BindView(R.id.tv_item_tag_details_things_like_count)
        TextView tvItemTagDetailsThingsLikeCount;
        @BindView(R.id.img_item_tag_details_things_like)
        ImageView imgItemTagDetailsThingsLike;

        ThingItemViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    class ArticlesViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.rlv_tag_details_article)
        NestingRecycleView rlvTagDetailsArticle;
        @BindView(R.id.tv_item_tag_details_check_all_article)
        TextView tvItemTagDetailsCheckAllArticle;
        View layout;
        TagDetailsArticlesRecycleViewAdapter adapter;
        ArticlesViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            this.layout = view;
            adapter = new TagDetailsArticlesRecycleViewAdapter(getContext());
            adapter.setMaxCount(2);
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
            linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            rlvTagDetailsArticle.setLayoutManager(linearLayoutManager);
            rlvTagDetailsArticle.setAdapter(adapter);
            tvItemTagDetailsCheckAllArticle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    categoryThingView.clickMoreArticles();
                }
            });
            adapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(int position, View view, Object data) {
                    categoryThingView.clickItemArticle((ArticlesBean) data);
                }
            });
        }

    }

    class ThingSortViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        @BindView(R.id.img_things_sort)
        ImageView imgThingsSort;
        View layout;
        ThingSortViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            this.layout = view;
            imgThingsSort.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            categoryThingView.clickThingSort();
        }
    }
}
